/* $Id: PageSubmit.java 138 2011-09-01 17:51:37Z altdotua@gmail.com $ */

package alt.djudge.frontend.client.ui.contest;

import alt.djudge.frontend.client.ui.AsyncCallbackSucc;
import alt.djudge.frontend.client.ui.ContentPage;
import alt.djudge.frontend.shared.dto.contest.ContestProblemInfoDTO;
import alt.djudge.frontend.shared.dto.contest.LanguageInfoDTO;
import alt.djudge.frontend.shared.dto.contest.ProblemStatementInfoDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;


public class PageSubmit extends Composite implements ContentPage
{
	interface MyUiBinder extends UiBinder<VerticalPanel, PageSubmit> {}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	@UiField
	VerticalPanel panel;
	
	@UiField
	Label lblTitle;
	
	@UiField
	ListBox lbProblem;
	
	@UiField
	ListBox lbLanguage;
	
	@UiField
	Button btnSubmit;
	
	@UiField
	Button btnClear;
	
	@UiField
	TextArea taSource;
	
	public PageSubmit()
	{
		initWidget(uiBinder.createAndBindUi(this));
		
		btnClear.addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				lbLanguage.setSelectedIndex(0);
				lbProblem.setSelectedIndex(0);
				taSource.setText("");
			}
		});
		
		btnSubmit.addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public void onDataLoaded(ProblemStatementInfoDTO[] data)
	{
		lblTitle.setText("Problem statement");
	}
	
	public void loadData()
	{
		AsyncCallback<ContestProblemInfoDTO[]> callback = Layout.getLayout().getAsyncCallback(new AsyncCallbackSucc<ContestProblemInfoDTO[]>()
		{
			@Override
			public void onSuccess(ContestProblemInfoDTO[] result)
			{
				lbProblem.clear();
				for (ContestProblemInfoDTO info : result)
				{
					lbProblem.addItem(info.getSid() + ": " + info.getName(), info.getId().toString());
				}
			}
		});
		
		AsyncCallback<LanguageInfoDTO[]> callback2 = Layout.getLayout().getAsyncCallback(new AsyncCallbackSucc<LanguageInfoDTO[]>()
			{
				@Override
				public void onSuccess(LanguageInfoDTO[] result)
				{
					lbLanguage.clear();
					for (LanguageInfoDTO info : result)
					{
						lbLanguage.addItem(info.getSid() + ": " + info.getName(), info.getId().toString());
					}
				}
			}
		);
		
		Layout.contestService.getProblems(Layout.getLayout().getContestId(), callback);
		Layout.contestService.getLanguages(Layout.getLayout().getContestId(), callback2);
	}

	@Override
	public void dispatch(String token)
	{
		loadData();
	}

	@Override
	public String getHistoryTokenPrefix()
	{
		return "submit";
	}
}

